﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using GeekShop.Commands;
using System.Drawing.Drawing2D;

namespace GeekShop.Tools.Select
{
    public class Rectangle : MouseBehaviour, ITool
    {
        #region Fields
        private Region region;
        private ICommand command;
        #endregion

        #region Properties
        public Cursor Cursor
        {
            get
            {
                return Cursors.SizeAll;
            }
        }
        public Bitmap Icon
        {
            get
            {
                return GeekShop.Properties.Resources.edit_cut;
            }
        }
        public string Name
        {
            get { return "Select Rectangle"; }
        }
        public ICommand Command { get { return this.command; } }
        #endregion

        #region Methods
        public void RunDialog(PictureManager pictureManager)
        {
            this.picture = pictureManager.SelectedPicture;
        }
        #endregion

        #region Events
        public event EventHandler ToolReady;
        #endregion

        #region Event Methods
        public override void MouseDown(object sender, MouseEventArgs e)
        {
            base.MouseDown(sender, e);
            base.preview = (Bitmap)base.picture.Bitmap.Clone();
        }
        public override void MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //todo: cancel dragging if escape is pressed
                base.buffer = (Bitmap)base.preview.Clone();
                Size size = new Size(e.Location.X - base.start.X, e.Location.Y - base.start.Y);
                Graphics g = Graphics.FromImage(base.buffer);
                Pen pen = new Pen(new HatchBrush(HatchStyle.DarkUpwardDiagonal, Color.White, Color.Black));
                
                #region Horizontal Lines
                g.DrawLine(pen,
                    base.start,
                    new Point(e.Location.X, this.start.Y)
                );
                g.DrawLine(pen,
                    new Point(base.start.X, e.Location.Y),
                    e.Location
                );
                #endregion
                #region Vertical Lines
                g.DrawLine(pen,
                    base.start,
                    new Point(base.start.X, e.Location.Y)
                );
                g.DrawLine(pen,
                    e.Location,
                    new Point(e.Location.X, base.start.Y)
                );
                #endregion

                base.graphics.DrawImage(base.buffer, 0, 0);
            }
        }
        public override void MouseUp(object sender, MouseEventArgs e)
        {
            Size size = new Size(Math.Abs(e.Location.X - base.start.X), Math.Abs(e.Location.Y - base.start.Y));
            Point topLeft = new Point(
                base.start.X < e.Location.X ? base.start.X : e.Location.X,
                base.start.Y < e.Location.Y ? base.start.Y : e.Location.Y
            );
            
            this.region = new Region(new System.Drawing.Rectangle(topLeft, size));
            this.command = new Commands.Select.Rectangle(base.picture, this.region);
            this.ToolReady(this, new EventArgs());
        }
        #endregion

    }
}
